<?php
namespace Home\Model;
use Think\Model;
class ArticleModel extends Model {
    public function addArticle($data){
        $this->add($data);
    }


    public function deleteArticleById($id){
        return $this->where("id='$id'")->delete();
    }
    public function deleteArticleByAuthorId($author_id){
        return $this->where("author_id='$author_id'")->delete();
    }

    public function updateArticleById($id,$data){
        return $this->where("id='$id'")->save($data);
    }

    public function updateArticleByCondition($id,$data){
        dump($this->where("id='$id'")->save($data));
    }

    public function getAllArticle(){
        $articles=$this->select();
        foreach($articles as $key => $article){
            $sort_data=D('Sort')->getSortById($article['sort_id']);
            $articles[$key]['sort']=$sort_data[0]['name'];
            $user_data=(D('User')->getUserById($article['author_id']));
            $articles[$key]['author']=$user_data[0]['username'];
            $comment_data=(D('Comment')->getCommentByArticleIdDesc($articles[$key]['id']));
            $articles[$key]['last_comment']=$comment_data[0]['content'];
            $articles[$key]['last_comment_user_id']=$comment_data[0]['from_id'];
            $last_comment_user=(D('User')->getUserById($comment_data[0]['from_id']));
            $articles[$key]['last_comment_user_name']=$last_comment_user[0]['username'];
            $Avatar_data=D('Avatar')->getAvatarById($articles[$key]['id']);
            $articles[$key]['avatar_name']=$Avatar_data[0]['name'];

        }
        return $articles;
    }
    public function getArticleByArticleId($id){
        return $this->where("id='$id'")->select();
    }
    public function getArticleById($id){
        $articles= $this->where("id='$id'")->select();
        return $articles;
    }

    public function getArticleByAuthorId($author_id){
        $articles= $this->where("author_id='$author_id'")->select();
        return $articles;
    }

    public function getArticleByUserId($user_id){
        $articles= $this->where("author_id='$user_id'")->select();
        return $articles;
    }

    public function getDetailArticleByArticleId($id){
        $articles= $this->where("id='$id'")->select();
        foreach($articles as $key => $article){
            $author=D('User')->getUserById($article['author_id']);
            $articles[$key]['article_author']=$author[0]['username'];
            $articles[$key]['user_type']=$author[0]['user_type'];
            $sort=D('Sort')->getSortById($article['sort_id']);
            $articles[$key]['sort_name']=$sort[0]['name'];
        }
        return $articles[0];
    }

    public function getArticleBySortId($sort_id){
        $articles= $this->where("sort_id='$sort_id'")->select();
        foreach($articles as $key => $article){
            $sort_data=D('Sort')->getSortById($article['sort_id']);
            $articles[$key]['sort']=$sort_data[0]['name'];
            $user_data=(D('User')->getUserById($article['author_id']));
            $articles[$key]['author']=$user_data[0]['username'];
            $comment_data=(D('Comment')->getCommentByArticleIdDesc($articles[$key]['id']));
            $articles[$key]['last_comment']=$comment_data[0]['content'];
            $articles[$key]['last_comment_user_id']=$comment_data[0]['from_id'];
            $last_comment_user=(D('User')->getUserById($comment_data[0]['from_id']));
            $articles[$key]['last_comment_user_name']=$last_comment_user[0]['username'];
            $Avatar_data=D('Avatar')->getAvatarById($articles[$key]['author_id']);
            $articles[$key]['avatar_name']=$Avatar_data[0]['name'];
//            dump($articles[$key]['avatar_name']);

        }
        return $articles;
    }

    public function getArticleByName($name){
        return $this->where("name='$name'")->select();
    }

    public function searchArticleByKeyword($Keyword){
        $data['title']  = array('like',"%$Keyword%");
//        $data['content']  = array('like',"%$Keyword%");
//        return D('Article')->where($data)->buildSql();
        return D('Article')->where($data)->order('id desc')->select();
    }

    public function setTop(){

    }

    public function unsetTop(){

    }

}